package com.zl.view;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.table.DefaultTableModel;

import com.zl.dao.BookTypeDao;
import com.zl.model.BookType;
import com.zl.util.ZLString;
import com.zl.util.ZLUtil;

import javax.swing.ImageIcon;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import java.awt.event.ActionEvent;
import javax.swing.JTextArea;
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class BookTypeManagerFrame extends JInternalFrame {
	private JTextField textField;
	private JTable table;
	private JTextField textField_1;
	private JTextField textField_2;
	private JTextArea textArea;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					BookTypeManagerFrame frame = new BookTypeManagerFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public BookTypeManagerFrame() {
		setTitle("图书类别管理");
		setClosable(true);
		setIconifiable(true);
		setBounds(100, 100, 595, 527);
		
		JLabel lblNewLabel = new JLabel("图书类别名称：");
		
		textField = new JTextField();
		textField.setColumns(10);
		
		JButton btnNewButton = new JButton("查询");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				bookTypeSelect(e);
			}
		});
		btnNewButton.setIcon(new ImageIcon(BookTypeManagerFrame.class.getResource("/images/search.png")));
		
		JScrollPane scrollPane = new JScrollPane();
		
		JLabel lblNewLabel_1 = new JLabel("图书类别信息");
		
		JLabel lblNewLabel_2 = new JLabel("编号：");
		
		textField_1 = new JTextField();
		textField_1.setBackground(Color.LIGHT_GRAY);
		textField_1.setEditable(false);
		textField_1.setColumns(10);
		
		JLabel lblNewLabel_3 = new JLabel("图书类别名称：");
		
		textField_2 = new JTextField();
		textField_2.setColumns(10);
		
		JLabel lblNewLabel_4 = new JLabel("图书类别描述：");
		
		textArea = new JTextArea();
		textArea.setLineWrap(true);
		
		JButton btnNewButton_1 = new JButton("修改");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				updateBookType(e);
			}
		});
		btnNewButton_1.setIcon(new ImageIcon(BookTypeManagerFrame.class.getResource("/images/modify.png")));
		
		JButton button = new JButton("删除");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				deleteBookType(e);
			}
		});
		button.setIcon(new ImageIcon(BookTypeManagerFrame.class.getResource("/images/delete.png")));
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.TRAILING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(85)
							.addComponent(lblNewLabel)
							.addGap(18)
							.addComponent(textField, GroupLayout.PREFERRED_SIZE, 234, GroupLayout.PREFERRED_SIZE)
							.addGap(18)
							.addComponent(btnNewButton))
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(54)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
								.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 469, GroupLayout.PREFERRED_SIZE)
								.addGroup(groupLayout.createSequentialGroup()
									.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
										.addComponent(lblNewLabel_1)
										.addComponent(lblNewLabel_4)
										.addComponent(lblNewLabel_2))
									.addGap(18)
									.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
										.addGroup(groupLayout.createSequentialGroup()
											.addComponent(textField_1, GroupLayout.PREFERRED_SIZE, 89, GroupLayout.PREFERRED_SIZE)
											.addGap(18)
											.addComponent(lblNewLabel_3)
											.addPreferredGap(ComponentPlacement.RELATED)
											.addComponent(textField_2))
										.addComponent(textArea, GroupLayout.DEFAULT_SIZE, 360, Short.MAX_VALUE)))))
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(76)
							.addComponent(btnNewButton_1)
							.addGap(33)
							.addComponent(button)))
					.addContainerGap(48, Short.MAX_VALUE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(40)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel)
						.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(btnNewButton))
					.addGap(18)
					.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 133, GroupLayout.PREFERRED_SIZE)
					.addGap(33)
					.addComponent(lblNewLabel_1)
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(textField_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblNewLabel_3)
						.addComponent(textField_2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblNewLabel_2))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel_4)
						.addComponent(textArea, GroupLayout.PREFERRED_SIZE, 88, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnNewButton_1)
						.addComponent(button))
					.addContainerGap(16, Short.MAX_VALUE))
		);
		
		table = new JTable();
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				tableRowSelected(e);
			}
		});
		table.setModel(new DefaultTableModel(
				new Object[][] {
				},
				new String[] {
					"\u7F16\u53F7", "\u56FE\u4E66\u7C7B\u522B\u540D\u79F0", "\u56FE\u4E66\u7C7B\u522B\u63CF\u8FF0"
				}
			) {
				boolean[] columnEditables = new boolean[] {
					false, false, false
				};
				public boolean isCellEditable(int row, int column) {
					return columnEditables[column];
				}
			});
		table.getColumnModel().getColumn(1).setPreferredWidth(110);
		table.getColumnModel().getColumn(2).setPreferredWidth(123);
		scrollPane.setViewportView(table);
		getContentPane().setLayout(groupLayout);

		this.setTableData(new BookType());//传一个空的bookType对象
	}


	/**
	 * 修改
	 * @param e
	 */
	private void updateBookType(ActionEvent e) {
		String id = textField_1.getText();
		String bookTypeName = textField_2.getText();
		String bookTypeDesc = textArea.getText();
	
		if (ZLString.isEmpty(id)) {
			JOptionPane.showMessageDialog(null, "请选择需要修改的图书类别");
			return;
		}
		
		if (ZLString.isEmpty(bookTypeName)) {
			JOptionPane.showMessageDialog(null, "图书类别名称不能为空");
			return;
		}
		
		boolean isAdmin = LoginFrame.loginSuccessUser.getIsAdmin();
		if (!isAdmin) {
			JOptionPane.showMessageDialog(null, "该用户暂无修改图书类别的权限");
			return;
		}
		
		BookType bookType = new BookType(Integer.parseInt(id), bookTypeName, bookTypeDesc);
		Connection connection = null;
		
		try {
			
			connection = ZLUtil.getConnect();
			int success = BookTypeDao.update(connection, bookType);
			
			if (success == 1) {
				JOptionPane.showMessageDialog(null, "修改成功！");
				
				//清空文本框
				this.textArea.setText("");
				this.textField_1.setText("");
				this.textField_2.setText("");
				
				//重置table数据
				this.setTableData(new BookType());
			} else {

				JOptionPane.showMessageDialog(null, "修改失败！");
			}
			
		} catch (Exception e2) {
			// TODO: handle exception
			
			e2.printStackTrace();
			JOptionPane.showMessageDialog(null, "修改失败！");
		} finally {
			
			try {
				connection.close();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		
	}
	
	/**
	 * 删除
	 * @param e
	 */
	private void deleteBookType(ActionEvent e) {

		String id = textField_1.getText();
		
		if (ZLString.isEmpty(id)) {
			JOptionPane.showMessageDialog(null, "请选择需要删除的图书类别");
			return;
		}
		
		boolean isAdmin = LoginFrame.loginSuccessUser.getIsAdmin();
		if (!isAdmin) {
			JOptionPane.showMessageDialog(null, "该用户暂无删除图书类别的权限");
			return;
		}
		
		Connection connection = null;
		
		try {
			
			connection = ZLUtil.getConnect();
			int success = BookTypeDao.delete(connection, id);
			
			if (success == 1) {

				JOptionPane.showMessageDialog(null, "删除成功！");
				//清空文本框
				this.textArea.setText("");
				this.textField_1.setText("");
				this.textField_2.setText("");
				
				//重置table数据
				this.setTableData(new BookType());
			} else {

				JOptionPane.showMessageDialog(null, "删除失败！");
			}

		} catch (Exception e2) {
			// TODO: handle exception
			
			e2.printStackTrace();
			JOptionPane.showMessageDialog(null, "删除失败！");
		} finally {
			
			try {
				connection.close();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
	}
	
	/**
	 * table行点击事件
	 * @param e
	 */
	private void tableRowSelected(MouseEvent e) {
		// TODO Auto-generated method stub
		//取点击的行号
		int selectedRow = table.getSelectedRow();
		//下面textfield设置数据
		textField_1.setText(String.valueOf(table.getValueAt(selectedRow, 0)));//通过行+列来取对应的数据
		textField_2.setText((String)table.getValueAt(selectedRow, 1));
		textArea.setText((String)table.getValueAt(selectedRow, 2));
		
	}

	/**
	 * 查询图书类别
	 * @param e
	 */
	private void bookTypeSelect(ActionEvent e) {
		// TODO Auto-generated method stub
		String bookTypeName = this.textField.getText();
		BookType bookType = new BookType();
		bookType.setBookTypeName(bookTypeName);
		this.setTableData(bookType);
	}
	
	/**
	 * 设置表格数据
	 * @param bookType
	 */
	private void setTableData(BookType bookType) {
		DefaultTableModel model = (DefaultTableModel) table.getModel();
		Connection connection = null;
		model.setRowCount(0);//清空数据
		
		try {
		
			connection = ZLUtil.getConnect();
			ResultSet set = BookTypeDao.select(connection, bookType);
			
			while (set.next()) {
				//设置集合
				Vector vector = new Vector();
				vector.add(set.getInt("id"));
				vector.add(set.getString("bookTypeName"));
				vector.add(set.getString("bookTypeDesc"));
				model.addRow(vector);
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			
		} finally {
			 try {
				connection.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
